﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Lifang.Dao;
using Lifang.Entites;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Specialized;

namespace Lifang.API.Controllers
{
    [Route("api/Article")]
    public class ArticleController : BaseController
    {
        [HttpGet("ValidList")]
        public JsonResult ValidList()
        {
            var dp = Pager;
            var nvc = new NameValueCollection();
            if (Request.Query != null)
            {
                foreach (var key in Request.Query.Keys)
                {
                    nvc.Add(key, Request.Query[key]);
                }
            }
            dp.OrderField = " Id DESC ";
            dp.PageSize = 1000;
            var list = new CommonQuery().SelectViewPagerByKey<DP_Article>("ArticleView", nvc, dp);
            Pager = dp;
            return Json(list);
        }

        [HttpGet("Detail")]
        public JsonResult GetById(int id)
        {
            var mod = new DapperHelper().SelectModel<DP_Article>(id, new SqlNote("wmg", "查询栏目列表"));
            return Json(mod);
        }

        [HttpGet("Save")]
        public JsonResult SaveOrUpdate(DP_Article mod)
        {

            mod = new DP_Article();
            mod.Id = 1;
            mod.CategoryId = 1;
            mod.CreateDateTime = DateTime.Now;
            mod.CreateUserName = "蔡小光";
            mod.CreateUserNo = "02773";
            mod.DataFlag = 1;
            mod.IsDel = 0;
            mod.SubTitle = "2日，莫文蔚在网上发自拍并附文：“一飞冲天，生日快乐！”照片中，她现身机场，素颜披散长发，戴着鸭舌帽，露笑容活力十足。";
            mod.Summary = "去年的同一天，莫文蔚也选择了出游来庆祝生日，不知今年会去哪里呢？";
            mod.Title = "莫文蔚生日晒素颜";
            mod.UpdateDateTime = DateTime.Now;
            mod.UpdateUserName = "蔡小光";
            mod.UpdateUserNo = "02773";            
            new ArticleDao(LoginUserModel).SaveOrUpdateMod(mod);
            return Json(mod);
        }

        
        [HttpPost("SaveOrUpdateDTO")]
        public JsonResult SaveOrUpdateDTO(string ArticleContentList, string Article, string toDelContentIds, string toDelDepContentIds)
        {

            ArticleDTO mod = new ArticleDTO();


            mod.Article = Newtonsoft.Json.JsonConvert.DeserializeObject<DP_Article>(Article);
            mod.ArticleContentList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<DP_ArticleContent>>(ArticleContentList);
            var toDelContentIdsList = toDelContentIds.Split(',').Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
            var toDelDepContentIdsList = toDelDepContentIds.Split(',').Where(x => !string.IsNullOrWhiteSpace(x)).ToList();

            new ArticleDao(LoginUserModel).SaveOrUpdateMod(mod.Article);
            Parallel.ForEach(mod.ArticleContentList, (item, state) => { new ArticleContentDao(LoginUserModel).SaveOrUpdateMod(item); });
            Parallel.ForEach(toDelContentIdsList, (item, state) => {
                var newMod = new DP_ArticleContent() { DataFlag = 0, Id = Convert.ToInt32(item) };
                new ArticleContentDao(LoginUserModel).SaveOrUpdateMod(newMod,new SqlNote("cxg","更新数据"), new List<string>() { DP_ArticleContentValue.DataFlag });
            });

            Parallel.ForEach(toDelDepContentIdsList, (item, state) => {
                var newMod = new DP_ArticleContent() { DataFlag = 0, IsDel = 1, Id = Convert.ToInt32(item) };
                new ArticleContentDao(LoginUserModel).SaveOrUpdateMod(newMod, new SqlNote("cxg", "更新数据"),new List<string>() { DP_ArticleContentValue.DataFlag, DP_ArticleContentValue.IsDel });
            });

            return Json(mod);
        }
    }
}
